package com.wb.mas.ui.login;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.wb.mas.R;
import com.wb.mas.app.Constants;
import com.wb.mas.data.DemoRepository;
import com.wb.mas.entity.RootApiBean;
import com.wb.mas.ui.base.viewmodel.ToolbarViewModel;
import com.wb.mas.ui.webview.WebViewActivity;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class LoginByPhoneViewModel extends ToolbarViewModel {
    public LoginByPhoneViewModel(@NonNull Application application, DemoRepository model) {
        super(application, model);
    }


    public UiChangeEvent ui = new UiChangeEvent();

    public class UiChangeEvent {
        public SingleLiveEvent btnCountingEvent = new SingleLiveEvent();
        public SingleLiveEvent btnNormalEvent = new SingleLiveEvent();
    }

    public ObservableField<String> inputContent = new ObservableField<>("");
    public ObservableField<String> nextBtnText = new ObservableField<>(getString(R.string.login_by_phone_acquire_code));
    public ObservableField<Boolean> isCommitEnable = new ObservableField<>(true);

    public BindingCommand onClickNextStep = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            String inputContent = LoginByPhoneViewModel.this.inputContent.get();
            if (!TextUtils.isEmpty(inputContent)) {
                if (inputContent.length() >= 11) { //请求接口，获取验证码；执行倒计时  逻辑
                    acquireSmsCodeData();

                }
            } else {
                ToastUtils.showShort(getString(R.string.auth_pi_please_input_hint));
            }
        }
    });

    public BindingCommand onClickContract = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("title", getString(R.string.privacy_contract));
            bundle.putString("url", Constants.PRIVACY_CONTRACT);
            startActivity(WebViewActivity.class, bundle);
        }
    });



    private void acquireSmsCodeData() {
        showDialog();
        Map<String, Object> params = new HashMap<>();
        params.put("mobile", LoginByPhoneViewModel.this.inputContent.get());
        model.getSmsCode(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<RootApiBean>() {
                    @Override
                    public void accept(RootApiBean infoResponse) throws Exception {
                        dismissDialog();
                        if (infoResponse != null && infoResponse.getStatus() == 200) {
                            Bundle data = new Bundle();
                            data.putString("phone_num", LoginByPhoneViewModel.this.inputContent.get());
                            startActivity(LoginByPhoneStep2Ac.class, data);
                        } else {
                            ToastUtils.showShort(infoResponse.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        dismissDialog();
                        KLog.e(throwable.getMessage());
                        ToastUtils.showShort(throwable.getMessage());
                    }
                });
    }


}
